package butines.game2;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.GameAction;
import butines.core.GameCore;
import butines.core.GameCursor;
import butines.core.GameImage;
import butines.core.InputCodes;
import butines.game2.entity.Sprite.Type;
import butines.game2.entity.Building;
import butines.game2.entity.Entity.Status;
import butines.game2.entity.Unit;

public class GameMain extends GameCore {

    public static void main(String[] args) {
        new GameMain().startup();
    }

    private GameAction debug = new GameAction("Debug", GameAction.DETECT_INITAL_PRESS_ONLY);
    private GameAction quit = new GameAction("Sair", GameAction.NORMAL);
    
    private Game game;
    
    private Unit unit;
    private Building building;
    private Building farm;
    
    private MouseHandler mouseHandler;
    
    @Override
    public void init() {

        defineActionKey(quit, InputCodes.KEY_ESC);
        defineActionKey(debug, InputCodes.KEY_F1);

        setWindow(640, 480);
        setRelativeMouseMode(false);
        setSystemCursorVisible(true);
        setFrameRate(30);
        setGameCursor(new GameCursor(new GameImage("datas/ui/cursor_arrow.png")));

        GameImage imgFootman = new GameImage("teste.png");
        GameImage imgBarracks= new GameImage("barracks.png");
        GameImage imgFarm= new GameImage("farm.png");
        GameImage imgSmallFire= new GameImage("small_fire.png");
        GameImage imgBigFire= new GameImage("big_fire.png");
        
        GameImage[] smallFire = imgSmallFire.split(32, 48, 6);
        GameImage[] bigFire = imgBigFire.split(48, 48, 10);
        
        unit = new Unit();
        unit.getAnimation().setSheetImage(imgFootman, 72, 72);
        unit.getAnimation().setFrameRate(0.2f);
        unit.getAnimation().add(Type.STAY, 0);
        unit.getAnimation().add(Type.MOVE, 1, 2, 3, 4);
        unit.getAnimation().add(Type.ATTACK, 5, 6, 7, 8);
        unit.getAnimation().add(Type.DIE, 9, 10, 11);
        unit.setMass(1);
        unit.setMaxForce(.5f);
        unit.setMaxSpeed(2.5f);
        unit.setMaxTurnRate(.15f);
        unit.setRadius(16);
        unit.getPosition().set(336, 272);
        unit.setStatus(Status.ALIVE);
        unit.setTeam(Team.RED);
        unit.setLife(50);
        unit.setHitPoints(100);
        unit.setSelectable(true);
        
        Unit unit2 = new Unit();
        unit2.getAnimation().setSheetImage(imgFootman, 72, 72);
        unit2.getAnimation().setFrameRate(0.2f);
        unit2.getAnimation().add(Type.STAY, 0);
        unit2.getAnimation().add(Type.MOVE, 1, 2, 3, 4);
        unit2.getAnimation().add(Type.ATTACK, 5, 6, 7, 8);
        unit2.getAnimation().add(Type.DIE, 9, 10, 11);
        unit2.setMass(1);
        unit2.setMaxForce(.5f);
        unit2.setMaxSpeed(2.5f);
        unit2.setMaxTurnRate(.15f);
        unit2.setRadius(16);
        unit2.getPosition().set(336, 336);
        unit2.setStatus(Status.ALIVE);
        unit2.setTeam(Team.RED);
        unit2.setLife(50);
        unit2.setHitPoints(100);
        unit2.setSelectable(true);
        
        building = new Building();
        building.setPassableGrid(Building.GRID_3_3_PLUS);
        building.setBuildableGrid(Building.GRID_3_3_SQUARE);
        building.setPosition(160, 160);
        building.setStatus(Status.ALIVE);
        building.setTeam(Team.RED);
        building.setCanAttack(false);
        building.setSelectable(true);
        building.setHitPoints(500);
        building.setLife(500);
        building.setSize(96, 96);
        building.setSheetImage(imgBarracks);
        building.useContructingImage();
        
        farm = new Building();
        farm.setPassableGrid(Building.GRID_2_2_SQUARE);
        farm.setBuildableGrid(Building.GRID_2_2_SQUARE);
        farm.getPosition().set(256, 256);
        farm.setStatus(Status.ALIVE);
        farm.setTeam(Team.RED);
        farm.setCanAttack(false);
        farm.setSelectable(true);
        farm.setHitPoints(500);
        farm.setLife(10);
        farm.setSize(64, 64);
        farm.setSheetImage(imgFarm);
        farm.useHealthyImage();
        
        Building building2 = new Building();
        building2.setPassableGrid(Building.GRID_3_3_PLUS);
        building2.setBuildableGrid(Building.GRID_3_3_SQUARE);
        building2.getPosition().set(256, 160);
        building2.setStatus(Status.ALIVE);
        building2.setTeam(Team.RED);
        building2.setCanAttack(false);
        building2.setSelectable(true);
        building2.setHitPoints(500);
        building2.setLife(200);
        building2.setSize(96, 96);
        building2.setSheetImage(imgBarracks);
        building2.useHealthyImage();
        
        game = new Game();
        game.setImgSmallFire(smallFire);
        game.setImgBigFire(bigFire);
        game.setTeam(Team.RED);
        game.addEntity(unit);
        game.addEntity(unit2);
        game.addEntity(building);
        game.addEntity(building2);
        game.addEntity(farm);
        
        mouseHandler = new MouseHandler();
        mouseHandler.setListener(game);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void update(float dt) {

        if (quit.isPressed()) {
            shutdown();
        }
        
        if (debug.isPressed()) {
            game.setDebug(!game.isDebug());
        }

        mouseHandler.processEvent(getMouse());
        
        game.update(getTime(), dt);
    }
    
    @Override
    public void render(Graphics2D g2d) {
        g2d.setBackground(Color.white);
        g2d.clearRect(0, 0, getWidth(), getHeight());
        
        Drawer.setGraphics(g2d);
        
        game.render(g2d);
    }
    
}
